﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PowerCreator.LMS.Web.User;
using System.Text;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_RoleManager_AuthorityUser : PowerCreator.LMS.Web.Base.BasePage
{
    public User opUser = new User();
    //Job opJob = new Job();
    //Duty opDuty = new Duty();
    Department opDepar = new Department();

    PowerCreator.LMS.Web.Action.UserRole opUserRole = new PowerCreator.LMS.Web.Action.UserRole();
    PowerCreator.LMS.Web.Action.Action opAction = new PowerCreator.LMS.Web.Action.Action();
    PowerCreator.LMS.Web.Action.UserAuthority opUserAuthority = new PowerCreator.LMS.Web.Action.UserAuthority();
    PowerCreator.LMS.Web.Action.AuthorityAction opAuthority = new PowerCreator.LMS.Web.Action.AuthorityAction();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索
            string authorityID = Request.QueryString["authorityID"].ToString();
            ViewState["authorityID"] = authorityID;
            BinderSex();
            lbl_AuthorityName.Text = opAuthority.LoadAuthority(Convert.ToInt32(authorityID)).AuthorityName;
            string AuthorityCount = string.Format("FkAuthorityID = {0}", ViewState["authorityID"]);
            lbl_AuthorityUserCount.Text = opAuthority.QueryUserAuthorityCount(string.Format("FkAuthorityID={0}", authorityID)).ToString();

            //  BinderJob();
            // BinderDuty();
            BinderDepartment();


            string search = GetSearch();
            BinderPager(search);
            BinderRpt(search, 1);
            InitPage();
        }
    }

    protected override void InitPage()
    {
        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_NoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_DelCheck.Text = GetLanageValueByKey("RoleUser_btn_Del", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_ShowAction.Text = GetLanageValueByKey("RoleUser_btn_showaction", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_UserAuthority.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o4", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_InfoTitle.Text = GetLanageValueByKey("RoleUser_lbl_title_t1", PowerCreator.LMS.Language.LanagetItemType.Page);
        string t = GetLanageValueByKey("RoleUser_lbl_title_t2", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AuthorityUserCount.Text = string.Format(t, lbl_AuthorityUserCount.Text);

        lbl_TeacherManager_Name.Text = GetLanageValueByKey("lbl_TeacherManager_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherManager_LoadingName.Text = GetLanageValueByKey("lbl_TeacherManager_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherManager_DepartmentName.Text = GetLanageValueByKey("lbl_TeacherManager_DepartmentName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherManager_Sex.Text = GetLanageValueByKey("lbl_TeacherManager_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_TeacherManager_JopName.Text = GetLanageValueByKey("lbl_TeacherManager_JopName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_TeacherManager_DutyName.Text = GetLanageValueByKey("lbl_TeacherManager_DutyName", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    #region 数据绑定
 
    //绑定部门信息
    private void BinderDepartment()
    {
        string search = "DepartmentType = 1";
        DataTable dt = opDepar.Query(search);
        ArrayList list = new ArrayList();
        list = opDepar.GetDepartmentTree(dt);

        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();

        ListItem li = new ListItem();
        li.Text = "不限部门";
        li.Value = "0";
        ddl_Department.Items.Insert(0, li);
    }
    protected void BinderSex()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "2";

        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "0";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "1";
        ddl_Sex.Items.Add(li);
        ddl_Sex.Items.Add(li0);
        ddl_Sex.Items.Add(li1);
    }
    #endregion

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();

        sbSearch.Append(string.Format(" AUser_Id in ( select FkUserID from TauthorityUser where FkAuthorityID = {0}) and ", ViewState["authorityID"]));

        if (ViewState["SearchType"].ToString() == "0")
        {
            if (!string.IsNullOrEmpty(txt_SearchKey.Text))
            {
                sbSearch.Append(string.Format(" AUserName like '%{0}%' or Name like '%{0}%' and ", txt_SearchKey.Text));
            }
        }
        else
        {
            if (!string.IsNullOrEmpty(txt_RealName.Text))
            {
                sbSearch.Append(string.Format(" AUserName like '%{0}%' and ", txt_RealName.Text));
            }
            if (!string.IsNullOrEmpty(txt_UserName.Text))
            {
                sbSearch.Append(string.Format(" Name like '%{0}%' and ", txt_UserName.Text));
            }

            if (ddl_Department.SelectedValue.ToString() != "0")
            {
                sbSearch.Append(string.Format("FKAdvancedDepartmentId = {0} and ", ddl_Department.SelectedValue.ToString()));
            }
            if (ddl_Sex.SelectedValue.ToString() != "2")
            {
                sbSearch.Append(string.Format(" Sex = {0} and ", ddl_Sex.SelectedValue.ToString()));
            }
        }



        if (sbSearch.ToString().EndsWith("and "))
            sbSearch.Remove(sbSearch.ToString().LastIndexOf("and "), 4);

        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = opUser.QueryVMAccountInfomationTeacherCount(search);
        AspNetPager1.PageSize = LMSPageSize;
    }
    private void BinderRpt(string search, int pageIndex)
    {
        DataTable dt = opUser.QueryVMAccountInfomaitionTeacher(search, LMSPageSize, pageIndex);
        rptUser.DataSource = dt;
        rptUser.DataBind();
        PreAspNetPager(AspNetPager1);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRpt(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        //搜索用户
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderRpt(search, 1);
    }
    protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "ldelauthority")
        {
            string searcht = string.Format(" FkUserID = {0} and FkAuthorityID = {1}", e.CommandArgument, ViewState["authorityID"]);
            DataTable dt = opUserAuthority.Query(searcht, opAuthority.QueryUserAuthorityCount(searcht), 1);
            if (dt.Rows.Count >= 1)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    opUserAuthority.Del(int.Parse(dt.Rows[i]["authorityUserID"].ToString()));
                }
                string search = GetSearch();
                BinderPager(search);
                BinderRpt(search, AspNetPager1.CurrentPageIndex);        
            }
        }
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rptUser.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRpt(search, 1);
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < rptUser.Items.Count; i++)
            {
                CheckBox cbox = (CheckBox)rptUser.Items[i].FindControl("Chbox_Check");
                Label lab_UserID = (Label)rptUser.Items[i].FindControl("lblid");
                if (cbox.Checked)
                {
                    string searcht = string.Format(" FkUserID = {0} and FkAuthorityID = {1}", lab_UserID.Text, ViewState["authorityID"]);
                    DataTable dt = opUserAuthority.Query(searcht, opUserAuthority.Count(searcht), 1);
                    if (dt.Rows.Count >= 1)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            opUserAuthority.Del(int.Parse(dt.Rows[j]["authorityUserID"].ToString()));
                        }
                        string search = GetSearch();
                        BinderPager(search);
                        BinderRpt(search, AspNetPager1.CurrentPageIndex);
                    }
                }
            }
            Script.Alert(this, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        catch
        {
            Script.Alert(this, "msg", GetLanageValueByKey("msg_CelueSelectQuest_shuruti24", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
    }
    protected void btn_ShowAction_Click(object sender, EventArgs e)
    {
        string url = string.Format("SetAuthorityAction.aspx?authorityID={0}", ViewState["authorityID"]);
        Script.RedirectURL(url);
    }
    protected void btn_UserRole_Click(object sender, EventArgs e)
    {
        string url = string.Format("authoritySelectUser.aspx?authorityID={0}", ViewState["authorityID"]);
        Script.RedirectURL(url);
    }
}
